package cn.fl.spl.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.product.service.ISplInfoService;
import cn.fl.product.service.ISplUsrRService;
import cn.fl.product.vo.SplInfoVO;
import cn.fl.product.vo.SplUsrRVO;
import cn.fl.spl.service.ISplCiCfgLogService;
import cn.fl.spl.vo.SplCiCfgLogVO;
import com.alibaba.fastjson.JSON;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.framework.enums.BasisDataRelDescEnum;
import fl.spl.facade.spl.api.ISplCommBlackListApiService;
import fl.spl.facade.spl.condition.SplCiCfgLogQC;
import fl.spl.facade.spl.condition.SplCommBlackListQC;
import fl.spl.facade.spl.condition.SplUsrRQC;
import fl.spl.facade.spl.constant.SplCommBlackListConstant;
import fl.spl.facade.spl.dto.SplCommBlackListDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;

/**
 * @Description: 供应商征信配置记录表 控制层
 */
@Controller
@RequestMapping("/spl/splcicfglog")
public class SplCiCfgLogController extends BaseController {

	/**
	 * SplCiCfgLog 服务层BEAN
	 */
	@Autowired
	private ISplCiCfgLogService splCiCfgLogService;

	@Autowired
	private ISplInfoService splInfoService;

	@Autowired
	private ISplCommBlackListApiService splCommBlackListApiService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();

		ModelAndView view = new ModelAndView("spl/splCiCfgLog/splCiCfgLogList");
		
		Long endTm = System.currentTimeMillis();
		return view;
	}
	
	/**
	 * 到新增修改页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();

		ModelAndView view = new ModelAndView("spl/splCiCfgLog/splCiCfgLogAdd");
		
		Long endTm = System.currentTimeMillis();
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();

		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("spl/splCiCfgLog/splCiCfgLogModify");
		view.addObject("id", id);
		Long endTm = System.currentTimeMillis();
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toSplCiCfgModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toSplCiCfgModifyPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("spl/splInfo/splCiConfigModify");
		//查询供应商基本信息
		String splId = request.getParameter("splId");
		SplInfoVO splVo = splInfoService.selectById(Long.valueOf(splId));

		view.addObject("isCheckCreditBlackList", 0);
		SplCommBlackListQC splCommBlackListQC = new SplCommBlackListQC();
		splCommBlackListQC.setIsDel(BaseConstant.IS_YESNO_NO);
		splCommBlackListQC.setIsValid(BaseConstant.IS_YESNO_YES);
		splCommBlackListQC.setUsrTypCd(BasisDataRelDescEnum.SPL.getTableSimpleNm());
		splCommBlackListQC.setBusTypCd(SplCommBlackListConstant.SHOP_APP_CHECKCREDIT_BLACK_LIST_BUS_TYP);
		splCommBlackListQC.setUsrId(Long.valueOf(splId));
		List<SplCommBlackListDTO> splCommBlackListDTOS = this.splCommBlackListApiService.searchSplCommBlackListDTOs(splCommBlackListQC);
		if(CollectionUtils.isNotEmpty(splCommBlackListDTOS)){
			view.addObject("isCheckCreditBlackList", 1);
		}
		view.addObject("splVo",splVo);
		view.addObject("splNm", splVo.getSplNm());
		view.addObject("splId", splId);
		return view;
	}
	
	/**
	 * 到详情页面
	 */
	@RequestMapping(value = "/toDetailPage.do", method = { RequestMethod.GET})
	public ModelAndView toDetailPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();

		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("spl/splCiCfgLog/splCiCfgLogDetail");
		view.addObject("id", id);
		
		Long endTm = System.currentTimeMillis();
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();

		
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String searchName = request.getParameter("demo.name");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		SplCiCfgLogQC qc = new SplCiCfgLogQC();
		if (StringUtils.isNotEmpty(searchName)) {
			//TODO SOMTHING
		}
		page.setCondition(qc);

		//TODO SOMETHING
		Pager pager = new Pager();//this.splCiCfgLogService.searchListPage(page);
		List<SplCiCfgLogVO> list = (List<SplCiCfgLogVO>) pager.getResultList();
		ListPageVO<SplCiCfgLogVO> listPage = new ListPageVO<SplCiCfgLogVO>(list, pager.getTotalCount());
		Long endTm = System.currentTimeMillis();
		return listPage;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initListByOneData.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initListByOneData(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String dataId = request.getParameter("dataId");
		String dataTabNm = request.getParameter("dataTabNm");

		if (StringUtils.isEmpty(dataId)) {
			BaseLogUtils.newWebLogger("数据主键ID不能为空").setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.info();
			return null;
		}
		if (StringUtils.isEmpty(dataTabNm)) {
			BaseLogUtils.newWebLogger("数据基础表名不能为空").setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.info();
			return null;
		}
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		SplCiCfgLogQC qc = new SplCiCfgLogQC();
		qc.setDataId(Long.parseLong(dataId));
		qc.setDataTabNm(dataTabNm);
		qc.setIsDel(BaseConstant.IS_YESNO_NO);

		page.setCondition(qc);

		Pager pager = this.splCiCfgLogService.searchListPage(page);
		List<SplCiCfgLogVO> list = (List<SplCiCfgLogVO>) pager.getResultList();
		ListPageVO<SplCiCfgLogVO> listPage = new ListPageVO<SplCiCfgLogVO>(list, pager.getTotalCount());
		return listPage;
	}
	
	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();

		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
		SplCiCfgLogVO entity = null;
		try {
			entity = new SplCiCfgLogVO();
			//TODO ADD PARAM
			Long id = this.splCiCfgLogService.add(entity);
			result.setData(id);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
		}
		Long endTm = System.currentTimeMillis();

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();

		String id = request.getParameter("id");
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("数据不存在!");
			return result;
		}
		SplCiCfgLogVO entity = null;
		try {
			entity = new SplCiCfgLogVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			//TODO ADD PARAM
			boolean success = this.splCiCfgLogService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改失败!");
			}
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
		}
		
		Long endTm = System.currentTimeMillis();

		return result;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();

		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			return result;
		}
		try {
			this.splCiCfgLogService.removeById(Long.valueOf(id));
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
		}
		
		Long endTm = System.currentTimeMillis();

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();

		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			return result;
		}
		
		try {
			SplCiCfgLogVO vo = this.splCiCfgLogService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("数据不存在!");
			}
			result.setData(vo);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
		}
		
		Long endTm = System.currentTimeMillis();

		return result;
	}
}

